package com.wisewin.pose.api.controller;

import com.wisewin.pose.api.controller.base.BaseController;
import com.wisewin.pose.config.dto.ResultDTO;
import com.wisewin.pose.config.dto.ResultDTOBuilder;
import com.wisewin.pose.entity.bo.QueryParamBO;
import com.wisewin.pose.entity.db.UserDO;
import com.wisewin.pose.entity.db.UserPhysicalRecordDO;
import com.wisewin.pose.service.UserPhysicalRecordService;
import com.wisewin.pose.service.UserPrescriptionService;
import com.wisewin.pose.util.PageUtil;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wisewin.pose.service.UserService;

@RestController
@RequestMapping("/record")
public class UserPhysicalRecordController extends BaseController {

    final static Logger log = LoggerFactory.getLogger(WechatController.class);

    @Autowired
    private UserPhysicalRecordService userPhysicalRecordService;
    @Autowired
    private UserPrescriptionService userPrescriptionService;

    @Autowired
    private UserService userService;

    // 保存报告
    @ApiOperation(value = "保存用户体测报告")
    @PostMapping("/saveUserPhysicalRecord")
    public ResultDTO saveUserPhysicalRecord(@RequestBody UserPhysicalRecordDO userPhysicalRecordDO) {
        if (StringUtils.isEmpty(userPhysicalRecordDO.getSessionId())) {
            return ResultDTOBuilder.failure("0000005");
        }
        // 获取登录用户
        UserDO loginUser = userService.queryByOpenId(userPhysicalRecordDO.getSessionId());
        log.info("save user {} ", loginUser);
        // UserDO loginUser = super.getLoginUser(userPhysicalRecordDO.getSessionId());
        if (loginUser == null) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001");
        }
        userPhysicalRecordDO.setUserId(loginUser.getId());
        return userPhysicalRecordService.saveUserPhysicalRecord(userPhysicalRecordDO);
    }

    // recordId, uid -> 查询单条报告记录
    @PostMapping("/getUserPhysicalRecord")
    public ResultDTO getUserPhysicalRecord(@RequestBody UserPhysicalRecordDO userPhysicalRecordDO) {
        if (StringUtils.isEmpty(userPhysicalRecordDO.getSessionId())) {
            return ResultDTOBuilder.failure("0000005");
        }
        if (userPhysicalRecordDO.getRecordId() == null) {
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 获取登录用户
        UserDO loginUser = super.getLoginUser(userPhysicalRecordDO.getSessionId());
        if (loginUser == null) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001");
        }
        userPhysicalRecordDO.setUserId(loginUser.getId());
        return userPhysicalRecordService.queryPhysicalRecord(userPhysicalRecordDO);
    }

    // uid -> 查询报告列表
    @PostMapping("/getPhysicalRecords")
    public ResultDTO getPhysicalRecord(@RequestBody QueryParamBO query) {
        if (StringUtils.isEmpty(query.getSessionId())) {
            return ResultDTOBuilder.failure("0000005");
        }
        if (query.getUserId() == null) {
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        // 获取登录用户
        UserDO loginUser = super.getLoginUser(query.getSessionId());
        if (loginUser == null) { // 判断是否登录
            return ResultDTOBuilder.failure("0000001");
        }
        // 处理机构ID
        query.setOrgId(query.getOrgId() == null ? 1 : query.getOrgId());
        query.setUserId(loginUser.getId());
        if (query.getPageNo() == null) {
            return ResultDTOBuilder.failure("1", "参数有误", null);
        }
        Long pageSize = (query.getPageSize() == null || query.getPageSize() <= 0) ? 10 : query.getPageSize();
        PageUtil util = getPageQuery(query.getPageNo(), pageSize);
        return userPhysicalRecordService.queryPhysicalRecords(query, util);
    }

    // 报告id, uid -> 查询报告详情
    @ApiOperation(value = "查询用户体测报告详情")
    @PostMapping("/getPhysicalRecordDetail")
    public ResultDTO getPhysicalRecordDetail(@RequestBody UserPhysicalRecordDO userPhysicalRecordDO) {
        // if (StringUtils.isEmpty(userPhysicalRecordDO.getSessionId())) {
        // return ResultDTOBuilder.failure("0000005");
        // }
        // if (userPhysicalRecordDO.getRecordId() == null) {
        // return ResultDTOBuilder.failure("1", "参数有误", null);
        // }
        // // 获取登录用户
        // UserDO loginUser = super.getLoginUser(userPhysicalRecordDO.getSessionId());
        // if (loginUser == null) { // 判断是否登录
        // return ResultDTOBuilder.failure("0000001");
        // }
        // userPhysicalRecordDO.setUserId(loginUser.getId());

        return userPhysicalRecordService.queryPhysicalRecordDetail(userPhysicalRecordDO);
    }

    @GetMapping("/dealUserPrescription")
    public ResultDTO dealUserPrescription(Integer recordId) {
        userPrescriptionService.dealUserPrescription(recordId);
        return ResultDTOBuilder.success(recordId);
    }
}
